package com.zxkj.commonpatrol.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.czy.basecommon.utils.base.BaseVmDataBindingActivity;
import com.czy.basecommon.utils.base.StateData;
import com.czy.basecommon.utils.tools.ActivityTool;
import com.jeremyliao.liveeventbus.LiveEventBus;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xuexiang.xui.widget.dialog.MiniLoadingDialog;
import com.xuexiang.xui.widget.dialog.materialdialog.MaterialDialog;
import com.xuexiang.xui.widget.toast.XToast;
import com.zxkj.commonpatrol.BR;
import com.zxkj.commonpatrol.R;
import com.zxkj.commonpatrol.adapt.CheckPointListAdapt;
import com.zxkj.commonpatrol.databinding.ActivityCheckPointDetailBinding;
import com.zxkj.commonpatrol.model.entity.CheckPointListEntity;
import com.zxkj.commonpatrol.model.entity.PointEvent;
import com.zxkj.commonpatrol.viewmodel.CheckPointDetailViewModel;

import java.util.List;

public class CheckPointDetailActivity extends BaseVmDataBindingActivity<ActivityCheckPointDetailBinding, CheckPointDetailViewModel> {

    private RecyclerView recyclerView;
    private CheckPointListAdapt checkPointListAdapt;
    private String routeInspectNo;
    private String pointNo;
    private String Filter;
    private SmartRefreshLayout smartRefreshLayout;
    private MiniLoadingDialog loadingDialog;
    private String RSName;
    private boolean isShow;

    @Override
    public int initContentView(Bundle savedInstanceState) {
        return R.layout.activity_check_point_detail;
    }

    @Override
    public int initVariableId() {
        return BR.CheckPointDetailViewModel;
    }

    @Override
    protected void setListener() {
        super.setListener();
        binding.ivSearch.setOnClickListener(v->{
            Bundle bundle=new Bundle();
            bundle.putInt("IsEdit",1);
            bundle.putString("RouteInspectNo",routeInspectNo);
            bundle.putString("PointNo",pointNo);
            bundle.putString("RSName",RSName);
            startActivity(AddDeciveActivity1.class,bundle);
//            new MaterialDialog.Builder(CheckPointDetailActivity.this)
//                    .title("新增裝置/事件")
//                    .items(R.array.menu_names)
//                    .itemsCallbackSingleChoice(
//                            0,
//                            (dialog, view, which, text) -> {
//                                if (which==0){
//                                    Bundle bundle=new Bundle();
//                                    bundle.putInt("IsEdit",1);
//                                    bundle.putString("RouteInspectNo",routeInspectNo);
//                                    bundle.putString("PointNo",pointNo);
//                                    bundle.putString("RSName",RSName);
//                                    startActivity(AddDeciveActivity1.class,bundle);
//                                }else{
//                                    Bundle bundle=new Bundle();
//                                    bundle.putString("PointNo",pointNo);
//                                    bundle.putString("RouteInspectNo",routeInspectNo);
//                                    bundle.putInt("IsEdit",1);
//                                    bundle.putString("RSName",RSName);
//                                    startActivity(AddEventActivity.class,bundle);
//                                }
//                                return true; // allow selection
//                            })
//                    .positiveText("確認")
//                    .negativeText("取消")
//                    .show();

        });
        binding.ivHeadSearch.setOnClickListener(v->{
            binding.multipleRejectStatusView.showLoading();
            Filter=binding.edSeach.getTextEx();
            viewModel.getDeciveEvent(routeInspectNo,pointNo,Filter);
        });
        smartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                binding.multipleRejectStatusView.showLoading();
                viewModel.getDeciveEvent(routeInspectNo,pointNo,Filter);
            }
        });
        binding.multipleRejectStatusView.setOnRetryClickListener(v -> {
            binding.multipleRejectStatusView.showLoading();
            viewModel.getDeciveEvent(routeInspectNo,pointNo,Filter);
        });
        binding.qkBack.setOnClickListener(v->{
            ActivityTool.getInstance().popOneActivity(this);
        });

        binding.edSeach.setOnFocusChangeListener((v,h)->{
            if (h){
              binding.ivHeadSearch.setImageResource(R.drawable.search_biss);
            }else{
                binding.ivHeadSearch.setImageResource(R.drawable.search);
            }
        });
        binding.btnSearch.setOnClickListener(v->{
            if (!isShow){
                binding.rlUpcoming.setVisibility(View.VISIBLE);
                isShow=true;
            }else{
                binding.rlUpcoming.setVisibility(View.GONE);
                isShow=false;
                Filter="";
                binding.edSeach.setTextEx("");
            }
        });

    }

    @Override
    protected void initUI() {
         recyclerView = (RecyclerView) findViewById(R.id.recy_check_point_detail);
         recyclerView.setLayoutManager(new LinearLayoutManager(this));
         smartRefreshLayout = (SmartRefreshLayout) findViewById(R.id.refreshLayout);
        //获取路线编号
         routeInspectNo = getIntent().getStringExtra("RouteInspectNo");
        //获取点编号
         pointNo = getIntent().getStringExtra("PointNo");
        String Name = getIntent().getStringExtra("Name");
        binding.tvTitle.setText(Name);


    }

   //    smartRefreshLayout.finishRefresh();
    //               if (loadingDialog!=null){
    //                   loadingDialog.dismiss();
    //               }

    @Override
    protected void initData() {
        if (loadingDialog==null){
            loadingDialog= new MiniLoadingDialog(this, "加載中...");
        }
        loadingDialog.show();

        viewModel.getDeciveEvent(routeInspectNo,pointNo,Filter);
    }

    @Override
    protected void dataObserver() {
        super.dataObserver();
        viewModel.RSName.observe(this, new Observer<StateData<String>>() {
            @Override
            public void onChanged(StateData<String> stringStateData) {
                RSName=stringStateData.getData();
            }
        });

//        LiveEventBus.get("add",Boolean.class).observe(this, new Observer<Boolean>() {
//            @Override
//            public void onChanged(Boolean aBoolean) {
//                initData();
//            }
//        });

//        viewModel.data.observe(this, new Observer<StateData<List<CheckPointListEntity>>>() {
//            @Override
//            public void onChanged(StateData<List<CheckPointListEntity>> listStateData) {
//                if (checkPointListAdapt==null){
//                    checkPointListAdapt = new CheckPointListAdapt();
//                    checkPointListAdapt.addData(listStateData.getData());
//                    recyclerView.setAdapter(checkPointListAdapt);
//                }else{
//
//                    checkPointListAdapt.setNewData(listStateData.getData());
//                }
//                checkPointListAdapt.setmDataCallBack(new CheckPointListAdapt.MyCheckPointListCallBack<Integer>() {
//                    @Override
//                    public void onDataSuccessCallBack(Integer o) {
//                        if (o==1){
//                            Bundle bundle=new Bundle();
//                            bundle.putInt("IsEdit",0);
//                            startActivity(AddDeciveActivity.class,bundle);
//                        }else{
//                            Bundle bundle=new Bundle();
//                            bundle.putInt("IsEdit",0);
//                            startActivity(AddEventActivity.class,bundle);
//                        }
//
//                    }
//
//                    @Override
//                    public void onDataFailCallBack(Integer o, Integer t1) {
//
//                    }
//                });
//
//
//            }
//        });


        viewModel.Datas.observe(this, new Observer<StateData<List<PointEvent>>>() {
            @Override
            public void onChanged(StateData<List<PointEvent>> listStateData) {
                smartRefreshLayout.finishRefresh();
                               if (loadingDialog!=null){
                                   loadingDialog.dismiss();
                               }
                if (listStateData.getStatus()== StateData.DataStatus.SUCCESS){
                    if (listStateData.getData().size()>0){
                        binding.multipleRejectStatusView.showContent();

                        if (checkPointListAdapt==null){
                            checkPointListAdapt = new CheckPointListAdapt(routeInspectNo,pointNo);
                            checkPointListAdapt.addData(listStateData.getData());
                            recyclerView.setAdapter(checkPointListAdapt);
                        }else{

                            checkPointListAdapt.setNewData(listStateData.getData());
                        }
                        checkPointListAdapt.setmDataCallBack(new CheckPointListAdapt.MyCheckPointListCallBack<Integer>() {
                            @Override
                            public void onDataSuccessCallBack(Integer o,Integer position) {
                                if (o==1){
                                    //   routeInspectNo = getIntent().getStringExtra("RouteInspectNo");
                                    //        //获取点编号
                                    //         pointNo = getIntent().getStringExtra("PointNo");
                                    Bundle bundle=new Bundle();
                                    bundle.putInt("IsEdit",0);
                                    bundle.putString("RouteInspectNo",routeInspectNo);
                                    bundle.putString("PointNo",pointNo);
                                    bundle.putString("DeviceNo",checkPointListAdapt.getData().get(position).getDevices().DevCode);
                                    startActivity(AddDeciveActivity1.class,bundle);
                                }else{
                                    Bundle bundle=new Bundle();
                                    bundle.putString("RouteInspectNo",routeInspectNo);
                                    bundle.putString("PointNo",pointNo);
                                    bundle.putString("EventNo",checkPointListAdapt.getData().get(position).getEvents().getCode());
                                    bundle.putInt("IsEdit",0);
                                    startActivity(AddEventActivity.class,bundle);
                                }

                            }

                            @Override
                            public void onDataFailCallBack(Integer o, Integer t1) {

                            }
                        });


                    }else{
                        binding.multipleRejectStatusView.showEmpty();
                    }
                }else{
                    binding.multipleRejectStatusView.showError();
                }
            }
        });

    }
}
